<?php
/**
	+----------------------------------------------------------
	* Ora 分页类
	* @Author: lan7  E-mail: upme7@163.com
	* @Ora 扩展库文件 2010-2-18
	+----------------------------------------------------------
*/
class Page extends base{
	var $maxPage;
	var $pageSize;
	var $currPage;
	var $total;
	var $sqlParam;
	var $leftNav   = array();
	var $middleNav = array();
	var $rightNav  = array();
	var $navSize;
	var $varName   = null;
	var $requestURI= null;
	var $rsort = false;
	/**
		+----------------------------------------------------------
		* 构造函数
		* Page(页面Id变量名称,当前页面值,总记录数,页面大小,导航大小)
		+----------------------------------------------------------
	*/
	function Page($varName,$currPage=1,$total,$pageSize=20,$navSize=5){
		$this->varName  = $varName;
		$this->total    = $total;
		$this->pageSize = $pageSize;
		$this->currPage = $currPage;
		$this->navSize  = $navSize;
		$this->init();
	}
	function __sleep(){
		$vars = (array)$this;
		foreach ($vars as $key => $val){
			if (is_null($val)){
				unset($vars[$key]);
			}
		}
		return array_keys($vars);
	}

	/**
		+----------------------------------------------------------
		* 初始化操作;
		+----------------------------------------------------------
	*/
	function init(){
		//初始化总页数
		if($this->total % $this->pageSize==0){
			$this->maxPage = $this->total / $this->pageSize;
		}else{
			$this->maxPage = intval($this->total / $this->pageSize)+1;
		}
		//页码控制
		if($this->currPage >= $this->maxPage){
			$this->currPage = $this->maxPage;
		}else if($this->currPage<=0){
			$this->currPage = 1;
		}
		//初始化SQL参数
		$this->sqlParam = ($this->currPage-1) * $this->pageSize.",".$this->pageSize;
		if($this->total<=0){
			$this->sqlParam = "0,0";
		}
		//使用倒序分页法
		if($this->currPage >= intval($this->maxPage/2) && $this->maxPage > 100){
			$this->sqlParam = ($this->maxPage-$this->currPage) * $this->pageSize.",".$this->pageSize;
			$this->rsort = true;
		}
		//导航Nav
		if(is_numeric($this->navSize)){
			//导航Nav left
			if($this->maxPage >= $this->navSize){
				for($i=1;$i<=$this->navSize;$i++){
					array_push($this->leftNav,$i);
				}
			}
			//导航Nav right
			if($this->maxPage >= $this->navSize*3){
				for($i=($this->maxPage-$this->navSize)+1;$i<=$this->maxPage;$i++){
					array_push($this->rightNav,$i);
				}
			}
			//中间Nav Middle
			$snum = $this->navSize+2;
			$enum = $this->maxPage - $this->navSize*2+1;
			if($this->currPage <= $snum){
				for($i=1;$i<=$this->navSize+1;$i++){
					array_push($this->middleNav,$i+$this->navSize);
				}
			}else if($this->currPage>$enum){
				for($i=$this->maxPage - $this->navSize*2;$i<=$this->maxPage-$this->navSize;$i++){
					array_push($this->middleNav,$i);
				}
			}
			else if($this->currPage > $snum & $this->currPage <= $enum){
				for($i=$this->currPage-2;$i<$this->currPage-1+$this->navSize;$i++){
					array_push($this->middleNav,$i);
				}
			}
			//处理少页面
			if($this->maxPage <= $this->navSize){
				for($i=1;$i<=$this->maxPage;$i++){
					array_push($this->leftNav,$i);
				}
				$this->middleNav = array();
				$this->rightNav  = array();
			}
		}
	}

	/**
		+----------------------------------------------------------
		* 下一页
		+----------------------------------------------------------
	*/
	function getNextPage($url=false){
		if($url){
			if($this->currPage<$this->maxPage){
				return $this->getPageUrl($this->currPage+1);
			}else{
				return $this->getPageUrl($this->currPage);
			}
		}else{
			if($this->currPage<$this->maxPage){
				return ($this->currPage+1);
			}else{
				return $this->currPage;
			}
		}
	}

	/**
		+----------------------------------------------------------
		* 上一页
		+----------------------------------------------------------
	*/
	function getPrevPage($url=false){
		if($url){
			if($this->currPage<=1){
				return $this->getPageUrl(1);
			}else{
				return $this->getPageUrl($this->currPage-1);
			}
		}else{
			if($this->currPage<=1){
				return 1;
			}else{
				return $this->currPage-1;
			}
		}
	}

	/**
		+----------------------------------------------------------
		* 最末页
		+----------------------------------------------------------
	*/
	function getLastPage($url=false){
		return $url?$this->maxPage:$this->getPageUrl($this->maxPage);

	}

	/**
		+----------------------------------------------------------
		* 获取最大页码
		+----------------------------------------------------------
	*/
	function getMaxPage(){
		return $this->maxPage;
	}

	/**
		+----------------------------------------------------------
		* 获取当前页码
		+----------------------------------------------------------
	*/
	function getCurrPage(){
		return $this->currPage;
	}

	/**
		+----------------------------------------------------------
		* 获取记录总数
		+----------------------------------------------------------
	*/
	function getTotal(){
		return $this->total;
	}

	/**
		+----------------------------------------------------------
		* 获取页面数量大小
		+----------------------------------------------------------
	*/
	function getPageSize(){
		return $this->pageSize;
	}
}
?>